package com.cwj.fron.start.controller.customer;

import com.cwj.common.entity.customer;
import com.cwj.common.entity.customer_log;
import com.cwj.common.utils.RedisUtil;
import com.cwj.common.viewObject.Vo;
import com.cwj.common.viewObject.fron.customer.viewObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class registerLoginController {
        private static final String  prefix="/axios_";
        @Resource
        private HttpServletRequest request;
        @Resource
        ObjectMapper objectMapper;
        @Resource
        RedisUtil redisUtil;
        @Resource
        private com.cwj.fron.customer.service.registerLogin registerLogin;
        @RequestMapping(path=prefix+"logincustomer",method = RequestMethod.POST)
        public Vo customer_login(@RequestBody customer customer) throws JsonProcessingException {
                 registerLogin.customer_login(customer);
                customer = (com.cwj.common.entity.customer) request.getAttribute("customer");
                customer_log customer_log =(com.cwj.common.entity.customer_log) request.getAttribute("customer_log");
                return new Vo()
                        .setCode(200)
                        .setData(
                                new viewObject()
                                        .setCustomer(customer)
                                        .setCustomer_log(customer_log)
                        );
        }
        @RequestMapping(path = prefix+"isupdatainsession",method = RequestMethod.GET)
        public Vo customer_isUpDataInSession(){
                if(request.getSession().getAttribute("customerId")==null) return  new Vo().setCode(400).setMsg("用户未登录");
                Integer customerId = (Integer) request.getSession().getAttribute("customerId");
                if(!Integer.valueOf(redisUtil.get("customerId" + customerId).toString()).equals(customerId))  return  new Vo().setCode(400).setMsg("用户未登录");
                customer customer = registerLogin.customer_isUpDataInSession(customerId);
                return null;
        }

}
